package entity

import (
	"errors"
	"fmt"
	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//OrganizationType 机构类型
type OrganizationType struct {
	ID   int    `gorm:"primary_key;column:TPNO"`
	Name string `gorm:"column:TPNM;size:10"`
}

//TableName 机构类型表名
func (OrganizationType) TableName() string {
	return "T_CONF_UTTP"
}

//ParentOrganization 机构
type ParentOrganization struct {
	ID   int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	Name string `gorm:"column:OrganName;size:20"`
}

func (ParentOrganization) TableName() string {
	return "T_Organization"
}

//Organization 机构
type Organization struct {
	ID          int                `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	No          string             `gorm:"column:OrganID;size:10"`
	ParentID    *int               `gorm:"column:ParentOrganID"`
	Parent      ParentOrganization `gorm:"ForeignKey:ParentOrganID"`
	Name        string             `gorm:"column:OrganName;size:20"`
	Description string             `gorm:"column:Description;size:30"`
	Type        OrganizationType   `gorm:"ForeignKey:UTTP"`
	TypeID      *int               `gorm:"column:UTTP"`
	OrderNo     int                `gorm:"column:OrderKey"`
}

//TableName Organization 机构表名
func (Organization) TableName() string {
	return "T_Organization"
}

//BeforeCreate CHAN
func (o *Organization) BeforeCreate(tx *gorm.DB) (err error) {
	var count int
	err = tx.Model(Organization{}).Where(&Organization{No: o.No}).Count(&count).Error
	if err != nil {
		return err
	}
	if count > 0 {
		info := fmt.Sprintf("机构号%s已存在，请修改！", o.No)
		return errors.New(info)
	}
	err = tx.Model(Organization{}).Where("OrganName=?", o.Name).Count(&count).Error
	if err != nil {
		return err
	}
	if count > 0 {
		info := fmt.Sprintf("机构名称%s已存在，请修改！", o.Name)
		return errors.New(info)
	}
	return
}

//AfterCreate 插入后的触发器
func (o *Organization) AfterCreate(tx *gorm.DB) (err error) {
	base.CacheDept(o.ID, o.Name, *o.ParentID, "")
	return
}
func (o *Organization) BeforeUpdate() (err error) {
	if o.ID == 0 {
		return errors.New("默认机构不允许修改！")
	}
	base.CacheDept(o.ID, o.Name, *o.ParentID, "")
	return
}
func OrganizationEF() base.EntityFunc {
	return func() interface{} {
		entity := Organization{}
		return &entity
	}
}
func OrganizationESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Organization
		return &entities
	}
}
func OrganizationTypeEF() base.EntityFunc {
	return func() interface{} {
		entity := OrganizationType{}
		return &entity
	}
}
func OrganizationTypeESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []OrganizationType
		return &entities
	}
}
func ParentOrganizationEF() base.EntityFunc {
	return func() interface{} {
		entity := ParentOrganization{}
		return &entity
	}
}
func ParentOrganizationESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ParentOrganization
		return &entities
	}
}

func init() {
	relationship := []string{"Parent", "Type"}
	base.DaoPoolObj.Put("Organization", base.NewDefaultDao(OrganizationEF(), OrganizationESF(), relationship))
	base.DaoPoolObj.Put("OrganizationType", base.NewDefaultDao(OrganizationTypeEF(), OrganizationTypeESF(), nil))
	base.DaoPoolObj.Put("ParentOrganization", base.NewDefaultDao(ParentOrganizationEF(), ParentOrganizationESF(), nil))
}
